package com.zzxx.wechat.result;

import lombok.Data;

@Data
public class R<T> {

    private Boolean success;
    private Integer code;
    private String message;
    private T data;

    private R() {
    }

    public static <T> R<T> ok() {
        R<T> r = new R<>();
        r.setSuccess(true);
        r.setCode(ResultEnum.SUCCESS.getCode());
        r.setMessage(ResultEnum.SUCCESS.getMsg());
        return r;
    }
    public static <T> R<T> ok(T data) {
        R<T> r = new R<>();
        r.setSuccess(true);
        r.setCode(ResultEnum.SUCCESS.getCode());
        r.setMessage(ResultEnum.SUCCESS.getMsg());
        r.setData(data);
        return r;
    }
    public static <T> R<T> error() {
        R<T> r = new R<>();
        r.setSuccess(false);
        r.setCode(ResultEnum.UNKNOWN_ERROR.getCode());
        r.setMessage(ResultEnum.UNKNOWN_ERROR.getMsg());
        return r;
    }
    public static <T> R<T> error(String message) {
        R<T> r = new R<>();
        r.setSuccess(false);
        r.setCode(ResultEnum.UNKNOWN_ERROR.getCode());
        r.setMessage(message);
        return r;
    }
    public static <T> R<T> error(ResultEnum resultEnum) {
        R<T> r = new R<>();
        r.setSuccess(false);
        r.setCode(resultEnum.getCode());
        r.setMessage(resultEnum.getMsg());
        return r;
    }
    public static <T> R<T> error(Integer code, String message) {
        R<T> r = new R<>();
        r.setSuccess(false);
        r.setCode(code);
        r.setMessage(message);
        return r;
    }


    public  R<T> success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public  R<T> message(String message) {
        this.setMessage(message);
        return this;
    }

    public R<T> code(Integer code) {
        this.setCode(code);
        return this;
    }

}
